<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <canvas id="canvas" width="400" height="400"></canvas>
  <button>点击关闭</button>
  <script>
    /** @type {HTMLCanvasElement} */
    var canvas=document.querySelector("#canvas")
    var ctx=canvas.getContext("2d")
    ctx.moveTo(100,100)
    ctx.lineTo(300,100)
    ctx.lineTo(300,300)
    ctx.setLineDash([5,5])
    var index=0
    var time
    function dh(){
      ctx.clearRect(0,0,400,400)
      index++
      ctx.lineDashOffset=index
      ctx.stroke()
      time=requestAnimationFrame(dh)
    }
    dh()
    var btn=document.querySelector("button")
    btn.onclick=function(){
      cancelAnimationFrame(time)
    }
  </script>
</body>
</html>